package com.kayang.token.controller;

import com.kayang.common.HttpMsg;
import com.kayang.nadapt.util.LogHelper;
import com.kayang.token.service.JwtService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
public class TokenController {

    @Resource
    JwtService jwtService;

    @RequestMapping("/getToken")
    public ResponseEntity getToken(@RequestParam String account, @RequestParam String password) {
        String token = jwtService.buildToken(account, password);
        HttpMsg callBackData = new HttpMsg();
        callBackData.setMsgId(0);
        callBackData.setMsg("操作成功");
        callBackData.setResult(token);
        LogHelper.login("生成用户上下文OK：account ：" + account);
        return ResponseEntity.ok(callBackData);
    }

    @RequestMapping("/checkToken")
    public ResponseEntity getToken(@RequestHeader String token) {
        String jsonUc = jwtService.checkToken(token);
        HttpMsg httpMsg = new HttpMsg();
        if(jsonUc != null && jsonUc.length() > 0){
            httpMsg.setMsgId(0);
            httpMsg.setMsg("认证成功");
            httpMsg.setResult(jsonUc);
            return ResponseEntity.ok(httpMsg);
        }else{
            httpMsg.setMsgId(-1);
            httpMsg.setMsg("认证失败");
            return ResponseEntity.status(403).body(httpMsg);
        }

    }

}
